package ma.controller.panels.parameterpanels;

import ma.controller.panels.auxillary.CtrInformationPanel;
import ma.controller.panels.auxillary.NumericalTFListener;
import ma.model.ManufacturingMethodInterface;
import ma.model.auxillary.Parameter;
import ma.view.gui.auxillary.NumericalTextField;

/**
 * The {@code CtrProcessPanel} controls and creates a sub
 * panel to hold parameter panels. A parameter panel consists of a parameter's
 * name, it's gui abbreviation, an input text field and it's unit. It is used
 * for process and machine dependent parameters. This class registers a
 * document listener for every text field to check if valid values were
 * entered.
 * @author Oliver Eckstein
 *
 */
public abstract class CtrProcessPanel
    extends CtrInPanel {
    
    /**
     * To create an instance of {@code ParameterPanelFrameController}.
     * @param manuMethod the manufacturing method whose parameter panels JPanel
     * should be created.
     */
    public CtrProcessPanel(
            ManufacturingMethodInterface manuMethod,
            CtrInformationPanel infoPanelCtr) {        
        super(manuMethod, infoPanelCtr);
    }
    
    @Override
    /*
     * (non-Javadoc)
     * @see ma.controller.panels2.parameterpanels.CtrParameterPanel#init()
     */
    protected void init() {        
        for (Parameter param : manuMethod.getProcessParameters()) {
            if(!param.getDependence()) {
                String[] paramInfo = new String[5];
                paramInfo[0] = param.getName();
                paramInfo[1] = param.getAbbreviation();
                paramInfo[2] = param.getGuiAbbreviation();
                paramInfo[3] = param.getUnit();
                Double paramValue = param.getValue();
                if(paramValue == null) {
                    paramInfo[4] = "";
                } else if (paramValue.equals(Double.NaN)) {
                    paramInfo[4] = "";    
                } else {
                    paramInfo[4] = paramInfo[4] = String.valueOf(paramValue);
                }                
                panel.addParameterPanel(paramInfo);
            }
        }               
    }
    
    @Override
    /*
     * (non-Javadoc)
     * @see ma.controller.panels.InputParameterPanelController#addTextFieldListenersAndRegisterObservers()
     */
    protected void addTextFieldListenersAndRegisterObservers() {
        Parameter param;
        String parameterAbbreviation;
        for(NumericalTextField textField : panel
                .getNonGeometricTextFields().values()) {
            parameterAbbreviation = textField.getIdentification();
            param = manuMethod.getParameter(parameterAbbreviation);
            textField.addDocumentListener(new NumericalTFListener(
                    textField, manuMethod, param, this));            
            param.registerObserver(panel);
        }
    }
}
